package ipdesk.model.report.consult.action;

import java.util.List;
import java.util.Map;

import ipdesk.model.report.consult.bean.ConsultInfoBean;
import ipdesk.model.report.consult.dao.ConsultInfoDao;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.PageUtil;

public class ConsultInfoSelectAction extends ActionSupport implements Preparable, ModelDriven<ConsultInfoBean>
{
	// 모델 드리븐에 사용되는 도메인 객체.
	private ConsultInfoBean consultInfoBean;
	
	// 상세보기 뷰페이지에서 사용되는 도메인 객체
	private ConsultInfoBean consultInfoDetail;
	
	// 페이지 정보를 저장하는 맵 객체
	private Map pageInfoDataMap;
	
	// 목록페이지에서 사용되는 도메인 객체
	private List<ConsultInfoBean> consultList;
	
	
	/**
	 * 목록 정보 조회.
	 */
	@Override
	public String execute() throws Exception
	{
		// 전체 데이터 갯수를 가져온다.
		int totalCount = ConsultInfoDao.getInstance().getDataTotalCount(consultInfoBean);
		
		
		
		// 페이지 정보 저장.
		pageInfoDataMap = PageUtil.getInstance().getPageInfoData(totalCount, consultInfoBean.getPageNumber());
		
		pageInfoDataMap.put("totalCount", totalCount);
		

		String deskCode = consultInfoBean.getDeskCode();
		String searchKeyword = consultInfoBean.getSearchKeyword();
		String order = consultInfoBean.getOrder();
		String year = consultInfoBean.getYear();
		
		// 넘어온 deskCode가 있다면
		if((deskCode != null) && (!deskCode.equals("")))
		{
			pageInfoDataMap.put("deskCode", consultInfoBean.getDeskCode());
		}
		
		if((order != null) && (!order.equals("")))
		{
			pageInfoDataMap.put("order", consultInfoBean.getOrder());
		}
		
		if((searchKeyword != null) && (!searchKeyword.equals("")))
		{
			pageInfoDataMap.put("searchOption", consultInfoBean.getSearchOption());
			pageInfoDataMap.put("searchKeyword", consultInfoBean.getSearchKeyword());
		}
		
		if((year != null) && (!year.equals("")))
		{
			pageInfoDataMap.put("year", consultInfoBean.getYear());
		}
		
		// 기업정보를 가지고 온다.
		consultList = ConsultInfoDao.getInstance().getPagingDataList(pageInfoDataMap);
		
		return SUCCESS;
	}
	
	/**
	 * 상세보기 데이터 가져오기
	 * @return
	 * @throws Exception
	 */
	public String executeDetail() throws Exception
	{
		/**
		 * 1. 목록에서 넘어온 ConsultingSeq를 조건으로 해당 데이터를 DB에서 가지고 온다.
		 * 2. 해당 사용자, 해당 지역코드는 Dao쪽에서 세션에 담긴 데이터를 가지고 온다.
		 */
		
		int consultSeq = consultInfoBean.getConsultingSeq();
		
		consultInfoDetail = ConsultInfoDao.getInstance().getDetailData(consultSeq);
		System.out.println("***"+consultInfoDetail.getConsultingSubTypeCode());
		
		return SUCCESS;
	}
	
	
	public Map getPageInfoDataMap()
	{
		return pageInfoDataMap;
	}

	public void setPageInfoDataMap(Map pageInfoDataMap)
	{
		this.pageInfoDataMap = pageInfoDataMap;
	}

	public List<ConsultInfoBean> getConsultList()
	{
		return consultList;
	}
	
	public void setConsultList(List<ConsultInfoBean> consultList)
	{
		this.consultList = consultList;
	}
	
	public ConsultInfoBean getConsultInfoDetail()
	{
		return consultInfoDetail;
	}

	public void setConsultInfoDetail(ConsultInfoBean consultInfoDetail)
	{
		this.consultInfoDetail = consultInfoDetail;
	}

	public void prepare() throws Exception
	{
		consultInfoBean = new ConsultInfoBean();
	}
	
	public ConsultInfoBean getModel()
	{
		return consultInfoBean;
	}
	
}
